﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using MAF.Common;

namespace MAF.CRUD
{
    /// <summary>
    /// 此类暂命名为CRUD
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class BusinessCRUD<T> : ICRUD<T> where T : BaseInfo
    {
        #region Protected Property

        protected ICRUD<T> DB = null;

        #endregion

        #region 构造方法

        public BusinessCRUD(ICRUD<T> db)
        {
            DB = db;
        }

        #endregion

        #region 虚方法

        public virtual void Add(T info)
        {
            DB.Add(info);
        }

        public virtual void Update(T info)
        {
            DB.Update(info);
        }

        public virtual int Delete(string ids)
        {
            return DB.Delete(ids);
        }

        public virtual T Show(Guid id)
        {
            return DB.Show(id);
        }

        public virtual IList<T> Index(BaseQueryVo vo)
        {
            return DB.Index(vo);
        }

        public int Count(BaseQueryVo vo)
        {
            return DB.Count(vo);
        }
        #endregion

        public BaseQueryVo ParseToQueryVo(NameValueCollection nvc)
        {
            BaseQueryVo vo = new BaseQueryVo();
            if (null != nvc)
            {
                vo.Name = nvc["Name"];
                vo.ParentId = nvc["ParentId"];
                vo.Ids = nvc["Ids"];
                vo.StringA = nvc["StringA"];
                vo.StringB = nvc["StringB"];
                vo.StringC = nvc["StringC"];

                string page = nvc["Page"];
                string size = nvc["PageSize"];

                if (null != page || null != size)
                {
                    vo.Page = new PageInfo(DB.Count(vo)
                                           , Utils.TryParse<int>(size)
                                           , Utils.TryParse<int>(page)
                                          );
                }
            }
            return vo;
        }
    }
}
